package com.training.admin.dao;

import java.util.List;

import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.persistence.annotation.Limit;
import com.demo2do.core.persistence.annotation.Query;
import com.demo2do.core.persistence.annotation.Variable;
import com.training.admin.entity.project.ProjectPhase;
import com.training.admin.entity.project.TrainingResult;
import com.training.admin.entity.trainee.Trainee;
/**
 * 
 * @author Rockett
 *
 */
@Dao("hibernate")
public interface TrainingResultDao {
	
	/**
	 * 
	 * @param projectId
	 * @return
	 */
	@Query("FROM TrainingResult WHERE projectPhase.id =:projectPhaseId")
	public List<TrainingResult> listByProjectPhaseId(@Variable("projectPhaseId") Long projectPhaseId);
	/**
	 * 
	 * @param projectId
	 * @return
	 */
	@Query("FROM TrainingResult WHERE projectPhase.id =:projectPhaseId and trainee.id =:traineeId")
	public List<TrainingResult> listByTraineeAndProjectPhase(@Variable("traineeId") Long traineeId,@Variable("projectPhaseId") Long projectPhaseId);
	/**
	 * 
	 * @param projectId
	 * @return
	 */
	@Query("FROM TrainingResult WHERE projectPhase.project.id =:projectId and trainee.id =:traineeId AND score IS NOT NULL")
	public List<TrainingResult> listByTraineeAndProject(@Variable("traineeId") Long traineeId,@Variable("projectId") Long projectId);
	/**
	 * 
	 * @param projectId
	 * @return
	 */
	@Query("select count(*) FROM TrainingResult t WHERE projectPhase.project.id =:projectId" )
	public int countByProject(@Variable("projectId") Long projectId);
	
	/**
	 * @param projectPhase
	 * @return
	 */
	@Query("SELECT COUNT(*) FROM TrainingResult WHERE projectPhase = :projectPhase" )
    public int countByProjectPhase(@Variable("projectPhase") ProjectPhase projectPhase);
	/**
	 * @param projectPhase
	 * @return
	 */
	@Limit
	@Query("FROM TrainingResult WHERE projectPhase = :projectPhase")
    public List<TrainingResult> listByProjectPhase(@Variable("projectPhase") ProjectPhase projectPhase, @Variable("beginIndex") int beginIndex, @Variable("maxResult") int maxResult);

	/**
	 * 
	 * @param traineeId
	 * @param projectPhaseId
	 * @return
	 */
	@Query("FROM TrainingResult WHERE projectPhase.id =:projectPhaseId and trainee.id =:traineeId")
	public TrainingResult loadByTraineeAndProjectPhase(@Variable("traineeId") Long traineeId, @Variable("projectPhaseId") Long projectPhaseId);
    
	/**
	 * @param emails
	 * @param projectPhaseId
	 * @return
	 */
	@Query("FROM TrainingResult WHERE trainee.email in :emails AND projectPhase.id =:projectPhaseId")
	public List<TrainingResult> loadByTraineeAndProject(@Variable("emails") List<String> emails, @Variable("projectPhaseId") Long projectPhaseId);
	
	/**
	 * 
	 * @param email
	 * @param projectId
	 * @return
	 */
	@Query("FROM TrainingResult WHERE trainee.email = :email AND projectPhase.disabled = false AND projectPhase.project.disabled = false")
	public List<TrainingResult> listByProject(@Variable("email") String email);
    
	/**
	 * 
	 * @param beginIndex
	 * @param maxResult
	 * @return
	 */
	@Query("FROM TrainingResult WHERE scanned = false")
	@Limit
	public List<TrainingResult> listByNotScanned(@Variable("beginIndex") int beginIndex, @Variable("maxResult") int maxResult);
    
	/**
	 * @param projectPhase
	 * @return
	 */
	@Query("FROM TrainingResult WHERE projectPhase = :projectPhase")
	public List<TrainingResult> listByProjectPhase(@Variable("projectPhase") ProjectPhase projectPhase);
	
	/**
	 * 
	 * @param projectPhase
	 * @param trainee
	 * @return
	 */
	@Query("FROM TrainingResult WHERE projectPhase = :projectPhase AND trainee = :trainee")
    public TrainingResult load(@Variable("projectPhase") ProjectPhase projectPhase, @Variable("trainee") Trainee trainee);
	
}
